#include <iostream>
#include <string>
#include <vector>
using namespace std;
int isPrime( int n){
    for(int i = 2 ;i < n/2 ; i++){
        if(n % i == 0){
            return 0;
        }
    }
    return 1;
}
int getNextPrime(int n){
    if(isPrime(n) == 1){
        return n;
    }
    return getNextPrime(n+1);
}
int isPrime_nazad( int n1){
    for(int i = 2 ;i < n1/2 ; i++){
        if(n1 % i == 0){
            return 0;
        }
    }
    return 1;
}
int getNextPrime_nazad(int n1){
    if(isPrime(n1) == 1){
        return n1;
    }
    return getNextPrime_nazad(n1-1);
}
int main(){
    string a;
    getline(cin, a);
    
    string ponk;
    int prob1  = a.find(' ',0);
    ponk = a.substr(prob1+1,a.length()-1);
    int prob2 = ponk.find(' ',0);
    //prob2 = prob2 + prob1+1;
    //cout << prob2 << endl;
    string chis1 = a.substr(0,prob1) , chis2 = ponk.substr(0,prob2),chis3 = a.substr(prob2+2+prob1,a.length()),clone_chis1 ="",clone_chis2="",clone_chis3="",glv = "";
    int glv_chis = 0;
    vector<string> popa = {"zero","one", "two" ,"three","four", "five","six","seven","eight","nine"};
    for(auto it : popa){          //chis1
        //cout << it << endl;
        if(chis1 == it){
            if(it == "zero"){
                clone_chis1=clone_chis1+"0";
            }
            else{
                if(it == "one"){
                    clone_chis1=clone_chis1+"1";
                }
                else{
                    if(it == "two"){
                        clone_chis1=clone_chis1+"2";
                    }
                    else{
                        if(it == "three"){
                            clone_chis1=clone_chis1+"3";
                        }
                        else{
                            if(it == "four"){
                                clone_chis1=clone_chis1+"4";
                            }
                            else{
                                if(it == "five"){
                                    clone_chis1=clone_chis1+"5";
                                }
                                else{
                                    if(it == "six"){
                                        clone_chis1=clone_chis1+"6";
                                    }
                                    else{
                                        if(it == "seven"){
                                            clone_chis1=clone_chis1+"7";
                                        }
                                        else{
                                            if(it == "eight"){
                                                clone_chis1=clone_chis1+"8";
                                            }
                                            else{
                                                if(it == "nine"){
                                                    clone_chis1=clone_chis1+"9";
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

    }
    for(auto it : popa){         // chis2
        //cout << it << endl;
        if(chis2 == it){
            if(it == "zero"){
                clone_chis2=clone_chis2+"0";
            }
            else{
                if(it == "one"){
                    clone_chis2=clone_chis2+"1";
                }
                else{
                    if(it == "two"){
                        clone_chis2=clone_chis2+"2";
                    }
                    else{
                        if(it == "three"){
                            clone_chis2=clone_chis2+"3";
                        }
                        else{
                            if(it == "four"){
                                clone_chis2=clone_chis2+"4";
                            }
                            else{
                                if(it == "five"){
                                    clone_chis2=clone_chis2+"5";
                                }
                                else{
                                    if(it == "six"){
                                        clone_chis2=clone_chis2+"6";
                                    }
                                    else{
                                        if(it == "seven"){
                                            clone_chis2=clone_chis2+"7";
                                        }
                                        else{
                                            if(it == "eight"){
                                                clone_chis2=clone_chis2+"8";
                                            }
                                            else{
                                                if(it == "nine"){
                                                    clone_chis2=clone_chis2+"9";
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

    }
    for(auto it : popa){         // chis3
        //cout << it << endl;
        if(chis3 == it){
            if(it == "zero"){
                clone_chis3=clone_chis3+"0";
            }
            else{
                if(it == "one"){
                    clone_chis3=clone_chis3+"1";
                }
                else{
                    if(it == "two"){
                        clone_chis3=clone_chis3+"2";
                    }
                    else{
                        if(it == "three"){
                            clone_chis3=clone_chis3+"3";
                        }
                        else{
                            if(it == "four"){
                                clone_chis3=clone_chis3+"4";
                            }
                            else{
                                if(it == "five"){
                                    clone_chis3=clone_chis3+"5";
                                }
                                else{
                                    if(it == "six"){
                                        clone_chis3=clone_chis3+"6";
                                    }
                                    else{
                                        if(it == "seven"){
                                            clone_chis3=clone_chis3+"7";
                                        }
                                        else{
                                            if(it == "eight"){
                                                clone_chis3=clone_chis3+"8";
                                            }
                                            else{
                                                if(it == "nine"){
                                                    clone_chis3=clone_chis3+"9";
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

    }
    glv = glv + clone_chis1 + clone_chis2 + clone_chis3;
    glv_chis = stoi(glv);
    int n = glv_chis,n1 = glv_chis;
    getNextPrime(n);
    getNextPrime_nazad(n1);
    if(getNextPrime(n)-glv_chis > glv_chis-getNextPrime_nazad(n1)){
        cout << getNextPrime_nazad(n1) << endl;
    }
    else{
        cout << getNextPrime(n) << endl;
    }
}